<template>
	<view class="container">
		<!-- 搜索框 -->
		<view class="search-container">
			<view class="search-box">
				<input 
					class="search-input" 
					placeholder="搜索服务" 
					v-model="searchKeyword"
					@confirm="handleSearch"
				/>
				<view class="search-btn" @click="handleSearch">搜索</view>
			</view>
		</view>
		
		<!-- 筛选栏 -->
		<view class="filter-container">
			<view class="filter-item" @click="showCategoryPicker">
				<text class="filter-text">{{currentCategoryName}}</text>
			</view>
			<view class="filter-item" @click="handleSortBySales">
				<text class="filter-text">销量</text>
				<text class="filter-arrow" :class="{'active': sortBy === 'service_sale'}">▼</text>
			</view>
			<view class="filter-item" @click="handleSortByPrice">
				<text class="filter-text">价格</text>
				<text class="filter-arrow" :class="{'active': sortBy === 'service_price'}">▼</text>
			</view>
		</view>
		
		<!-- 有数据时显示服务列表 -->
		<view v-if="list && list.length > 0" class="product-list">
			<view 
				class="product-item" 
				v-for="(item,index) in list" 
				@click="$jump.navigateTo('/pages/shops/detail?id=' + item.service_id)"
			>
				<image :src="item.service_image" class="product-image"></image>
				<view class="product-info">
					<view class="product-name">{{item.service_name}}</view>
					<view class="product-price">￥{{item.service_price}}</view>
					<view class="product-sales">销量: {{item.service_sale || 0}}</view>
				</view>
			</view>
		</view>
		
		<!-- 无数据时显示空状态 -->
		<view v-else class="empty-container">
			<view class="empty-text">暂无商品</view>
			<view class="empty-desc">该分类下暂时没有商品，请稍后再来看看~</view>
		</view>
	</view>
</template>

<script>
	
	export default{
		data(){
			return{
				list:[],
				searchKeyword: '',
				categoryId: '',
				currentCategoryName: '全部分类',
				sortBy: '', // 排序方式：'sales' 或 'price'
				sortOrder: 'desc' // 排序顺序：'asc' 或 'desc'
			}
		},
		onLoad(params){
			console.log(params)
			this.categoryId = params.id
			// 如果有关键词参数，设置到搜索框中
			if(params.keyWord) {
				this.searchKeyword = params.keyWord
			}
			// 设置分类名称
			if(params.categoryName) {
				this.currentCategoryName = decodeURIComponent(params.categoryName)
			}
			this.getList(params.id, params.keyWord || '')
		},
		methods:{
			async getList(id, keyword="", sortBy="", sortOrder="desc"){
				const requestData = {
					keyword
				}
				// 如果分类ID不为0，则添加分类ID参数
				if(id && id !== '0') {
					requestData.category_id = id
				}
				
				// 添加排序参数
				if (sortBy) {
					requestData.sort_by = sortBy
					requestData.sort_order = sortOrder
				}
				
				const res = await this.$service({
					url:"/api/member.service/getServiceList",
					data: requestData
				})
				console.log(res)
				this.list = res.data
			},
			
			handleSearch(){
				this.getList(this.categoryId, this.searchKeyword, this.sortBy, this.sortOrder)
			},
			
			// 显示分类选择器
			showCategoryPicker() {
				// 这里可以添加分类选择器的逻辑
				// 暂时只是提示
				uni.showToast({
					title: '分类选择功能待实现',
					icon: 'none'
				})
			},
			
			// 按销量排序
			handleSortBySales() {
				if (this.sortBy === 'service_sale') {
					// 如果已经是按销量排序，则切换排序顺序
					this.sortOrder = this.sortOrder === 'desc' ? 'asc' : 'desc'
				} else {
					// 第一次按销量排序，默认降序
					this.sortBy = 'service_sale'
					this.sortOrder = 'desc'
				}
				this.getList(this.categoryId, this.searchKeyword, this.sortBy, this.sortOrder)
			},
			
			// 按价格排序
			handleSortByPrice() {
				if (this.sortBy === 'service_price') {
					// 如果已经是按价格排序，则切换排序顺序
					this.sortOrder = this.sortOrder === 'desc' ? 'asc' : 'desc'
				} else {
					// 第一次按价格排序，默认升序
					this.sortBy = 'service_price'
					this.sortOrder = 'asc'
				}
				this.getList(this.categoryId, this.searchKeyword, this.sortBy, this.sortOrder)
			}
		}
	}
</script>

<style scoped>
	.container {
		background-color: #f5f5f5;
		min-height: 100vh;
	}
	
	/* 搜索框样式 */
	.search-container {
		background-color: #fff;
		padding: 20rpx 30rpx;
		border-bottom: 1rpx solid #eee;
	}
	
	.search-box {
		display: flex;
		align-items: center;
		background-color: #f8f8f8;
		border-radius: 50rpx;
		padding: 0 20rpx;
	}
	
	.search-input {
		flex: 1;
		height: 70rpx;
		font-size: 28rpx;
		color: #333;
	}
	
	.search-btn {
		padding: 15rpx 30rpx;
		background-color: #007aff;
		color: #fff;
		border-radius: 35rpx;
		font-size: 28rpx;
		margin-left: 20rpx;
	}
	
	/* 筛选栏样式 */
	.filter-container {
		display: flex;
		background-color: #fff;
		padding: 20rpx 0;
		border-bottom: 1rpx solid #eee;
	}
	
	.filter-item {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
	}
	
	.filter-text {
		font-size: 28rpx;
		color: #333;
		margin-right: 10rpx;
	}
	
	.filter-arrow {
		font-size: 20rpx;
		color: #999;
		transition: transform 0.3s ease;
	}
	
	.filter-arrow.active {
		color: #007aff;
		transform: rotate(180deg);
	}
	
	/* 商品列表样式 */
	.product-list {
		padding: 20rpx 30rpx;
	}
	
	.product-item {
		display: flex;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
	}
	
	.product-image {
		width: 160rpx;
		height: 160rpx;
		border-radius: 15rpx;
		margin-right: 30rpx;
		flex-shrink: 0;
	}
	
	.product-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}
	
	.product-name {
		font-size: 32rpx;
		color: #333;
		font-weight: 500;
		line-height: 1.4;
		margin-bottom: 20rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}
	
	.product-price {
		font-size: 36rpx;
		color: #ff4757;
		font-weight: bold;
		margin-bottom: 15rpx;
	}
	
	.product-sales {
		font-size: 26rpx;
		color: #999;
	}
	
	/* 空状态样式 */
	.empty-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 200rpx 40rpx;
		text-align: center;
	}
	
	.empty-image {
		width: 300rpx;
		height: 300rpx;
		margin-bottom: 40rpx;
	}
	
	.empty-text {
		font-size: 32rpx;
		color: #333;
		font-weight: 500;
		margin-bottom: 20rpx;
	}
	
	.empty-desc {
		font-size: 28rpx;
		color: #999;
		line-height: 1.5;
	}
</style>